// AFAppDotNetAPIClient.h
//
// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "JFBAPIClient.h"
#import "JFBGlobalSetting.h"

@interface JFBAPIClient()
@property(nonatomic, strong) JFBGlobalSetting *setting;
@property(nonatomic, strong) JFBAPIClient *client;
@property(nonatomic, assign) BOOL isLoginRequire;
@end

@implementation JFBAPIClient

+ (instancetype)sharedClient:(BOOL)loginRequire {
    static JFBAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    NSString * const JFBAPIClientAPIBaseURLString = [JFBGlobalSetting getSetting].rootURL;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[JFBAPIClient alloc] initWithBaseURL:[NSURL URLWithString:JFBAPIClientAPIBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });
    _sharedClient.isLoginRequire = loginRequire;
    return _sharedClient;
}

- (NSString*)buildURL:(NSString*)url{
    JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
    return [NSString stringWithFormat:@"%@%@", setting.rootURL, url];
}

- (id)buildParameters:(NSDictionary*)parameters{
    JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
    if( self.isLoginRequire ) {
        if( parameters ) {
            NSMutableDictionary * params = [parameters mutableCopy];
            [params setObject:[UserInfo currentUser].ut forKey:@"ut"];
            parameters = [NSDictionary dictionaryWithDictionary:params];
        } else {
            parameters = @{
//                           @"anycode":setting.accessToken,
                           @"shop_id":[NSString stringWithFormat:@"%lu", (unsigned long)setting.shopId]};
        }
    } else {
        if( parameters ) {
            NSMutableDictionary * params = [parameters mutableCopy];
//            [params setObject:setting.defaultAnyCode forKey:@"anycode"];
            parameters = [NSDictionary dictionaryWithDictionary:params];
        } else {
            parameters = @{@"anycode":setting.defaultAnyCode};
        }
        
    }
    NSLog(@"Parameter:%@", parameters);
    return parameters;
}

- (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(id)parameters
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    NSURLSessionDataTask * task = [super GET:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}
- (NSURLSessionDataTask *)HEAD:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(NSURLSessionDataTask *task))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super HEAD:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super POST:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
     constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super POST:[self buildURL:URLString] parameters:[self buildParameters:parameters] constructingBodyWithBlock:block success:success failure:failure];
    return task;
}

- (NSURLSessionDataTask *)PUT:(NSString *)URLString
                   parameters:(id)parameters
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super PUT:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}

- (NSURLSessionDataTask *)PATCH:(NSString *)URLString
                     parameters:(id)parameters
                        success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                        failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super PATCH:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}

- (NSURLSessionDataTask *)DELETE:(NSString *)URLString
                      parameters:(id)parameters
                         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSURLSessionDataTask * task = [super DELETE:[self buildURL:URLString] parameters:[self buildParameters:parameters] success:success failure:failure];
    return task;
}

@end
